package com.shop.cloud.controller.rebate;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.RebateConfig;
import com.shop.cloud.domain.hongfu.RebateHistory;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.service.hongfu.IRebateConfigService;
import com.shop.cloud.service.hongfu.IRebateHistoryService;
import com.shop.cloud.service.hongfu.IUserService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 返利管理
 * @author Yan Weiwei
 * 
 */
@RestController
@RequestMapping(value = "rebate")
public class RebateManageController extends BaseController {

	@Autowired
	private IRebateHistoryService rebateHistoryService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IRebateConfigService rebateConfigService;
	
	@ApiOperation(value = "获取返利列表", notes = "获取返利列表")
	@RequestMapping(value = "/rebateManageList", method = RequestMethod.POST)
	@ResponseBody
	public Response rebateManageList(@CurrentUserId Long userId, @RequestBody RebateHistory rebateHistory){
		
		rebateHistory.setOrderBy("REBATE_HISTORY.create_time DESC");
		Page<RebateHistory> data = rebateHistoryService.page(rebateHistory);
		
		List<RebateHistory> list = data.getData();
		if(null != list && list.size() > 0){
			for(RebateHistory insHistory : list){
				Long insUserId = insHistory.getUserId();
				User insUser = userService.get(insUserId);
				if(null != insUser){
					insHistory.setUserNickName(insUser.getNickname());
				}
				int year = insHistory.getThisYear();
				int month = insHistory.getThisMonth();
				insHistory.setShowYearAndMonth(year+"年"+month+"月账单");
			}
		}
		data.setData(list);
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "获取返利用户", notes = "获取返利用户")
	@RequestMapping(value = "/rebateUserMsg", method = RequestMethod.POST)
	@ResponseBody
	public Response rebateUserMsg(@CurrentUserId Long userId, @RequestBody RebateHistory rebateHistory){
		
		User data = userService.get(rebateHistory.getUserId());
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "获取返利商城购买限制", notes = "获取返利商城购买限制")
	@RequestMapping(value = "/rebateLimit", method = RequestMethod.POST)
	@ResponseBody
	public Response rebateLimit(@CurrentUserId Long userId){
		
		RebateConfig data = rebateConfigService.get(new Long(1));
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "修改返利商城购买限制", notes = "修改返利商城购买限制")
	@RequestMapping(value = "/modifyRebateLimit", method = RequestMethod.POST)
	@ResponseBody
	public Response modifyRebateLimit(@CurrentUserId Long userId, @RequestBody RebateConfig rebateConfig){
		
		RebateConfig data = rebateConfigService.get(new Long(1));
		if(null != data){
			data.setRebateLimit(rebateConfig.getRebateLimit());
			rebateConfigService.update(data);
		}
		
		return ResponseUtil.ok(0);
	}
}
